//
//  LGRadarChartViewController.m
//  LGCharts
//
//  Created by lg on 2019/5/16.
//  Copyright © 2019 lg. All rights reserved.
//

#import "LGRadarChartViewController.h"

@interface LGRadarChartViewController () <ChartViewDelegate, IChartAxisValueFormatter>

@property (nonatomic, strong) RadarChartView *radarChartView;

@end

@implementation LGRadarChartViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"LGRadarChartViewController";
    self.view.backgroundColor = UIColor.whiteColor;
    
    //创建雷达图对象
    self.radarChartView = [[RadarChartView alloc] initWithFrame:CGRectMake(20, 200, [UIScreen mainScreen].bounds.size.width-40, 300)];
    [self.view addSubview:self.radarChartView];
    self.radarChartView.delegate = self;
    self.radarChartView.rotationEnabled = YES;//是否允许转动
    self.radarChartView.highlightPerTapEnabled = YES;//是否能被选中
    self.radarChartView.legend.enabled = NO;//是否显示图例
    
    //雷达图线条样式
    self.radarChartView.webLineWidth = 0.5;//主干线线宽
    self.radarChartView.webColor = HexColor(0xc2ccd0);//主干线线宽
    self.radarChartView.innerWebLineWidth = 0.375;//边线宽度
    self.radarChartView.innerWebColor = HexColor(0xc2ccd0);//边线颜色
    self.radarChartView.webAlpha = 1;//透明度
    
    //设置 xAxi
    ChartXAxis *xAxis = self.radarChartView.xAxis;
    xAxis.labelFont = [UIFont systemFontOfSize:15];//字体
    xAxis.labelTextColor = HexColor(0x057748);//颜色
    xAxis.valueFormatter = self;
    
    //设置 yAxis
    ChartYAxis *yAxis = self.radarChartView.yAxis;
    yAxis.axisMinimum = 0.0;//最小值
    yAxis.axisMaximum = 100.0;//最大值
    yAxis.drawLabelsEnabled = YES;//是否显示 label
    yAxis.labelCount = 5;// label 个数
    yAxis.labelFont = [UIFont systemFontOfSize:9];// label 字体
    yAxis.labelTextColor = [UIColor lightGrayColor];// label 颜色
    
    //为雷达图提供数据
    self.radarChartView.data = self.radarData;
    [self.radarChartView renderer];
    
    //设置动画
    [self.radarChartView animateWithYAxisDuration:2.0f];
}

- (id)radarData {
    NSArray *entries1 = @[[[RadarChartDataEntry alloc] initWithValue:10],
                          [[RadarChartDataEntry alloc] initWithValue:80],
                          [[RadarChartDataEntry alloc] initWithValue:40],
                          [[RadarChartDataEntry alloc] initWithValue:50],
                          [[RadarChartDataEntry alloc] initWithValue:30],
                          [[RadarChartDataEntry alloc] initWithValue:70]];
    
    RadarChartDataSet *set1 = [[RadarChartDataSet alloc] initWithValues:entries1 label:@"呵呵呵呵"];
    [set1 setColor:[UIColor colorWithRed:103/255.0 green:110/255.0 blue:129/255.0 alpha:1.0]];
    set1.fillColor = [UIColor colorWithRed:103/255.0 green:110/255.0 blue:129/255.0 alpha:1.0];
    set1.drawFilledEnabled = YES;
    set1.fillAlpha = 0.7;
    set1.lineWidth = 2.0;
    set1.drawHighlightCircleEnabled = YES;
    [set1 setDrawHighlightIndicators:NO];
    
    NSArray *entries2 = @[[[RadarChartDataEntry alloc] initWithValue:20],
                          [[RadarChartDataEntry alloc] initWithValue:60],
                          [[RadarChartDataEntry alloc] initWithValue:80],
                          [[RadarChartDataEntry alloc] initWithValue:40],
                          [[RadarChartDataEntry alloc] initWithValue:50],
                          [[RadarChartDataEntry alloc] initWithValue:90]];
    
    RadarChartDataSet *set2 = [[RadarChartDataSet alloc] initWithValues:entries2 label:@"哈哈哈哈"];
    [set2 setColor:[UIColor colorWithRed:121/255.0 green:162/255.0 blue:175/255.0 alpha:1.0]];
    set2.fillColor = [UIColor colorWithRed:121/255.0 green:162/255.0 blue:175/255.0 alpha:1.0];
    set2.drawFilledEnabled = YES;
    set2.fillAlpha = 0.7;
    set2.lineWidth = 2.0;
    set2.drawHighlightCircleEnabled = YES;
    [set2 setDrawHighlightIndicators:NO];
    
    RadarChartData *data = [[RadarChartData alloc] initWithDataSets:@[set1,set2]];
    [data setValueFont:[UIFont fontWithName:@"HelveticaNeue-Light" size:8.f]];
    [data setDrawValues:YES];
    data.valueTextColor = UIColor.whiteColor;
    
    return data;
}

#pragma mark - IChartAxisValueFormatter

- (NSString *)stringForValue:(double)value axis:(ChartAxisBase *)axis {
    return @[@"hh", @"aa", @"bb", @"dd", @"ee", @"kk"][(int) value % 6];
}

#pragma mark - ChartViewDelegate

- (void)chartValueSelected:(ChartViewBase * __nonnull)chartView entry:(ChartDataEntry * __nonnull)entry highlight:(ChartHighlight * __nonnull)highlight
{
    NSLog(@"chartValueSelected");
}

- (void)chartValueNothingSelected:(ChartViewBase * __nonnull)chartView
{
    NSLog(@"chartValueNothingSelected");
}

@end
